﻿
(function () {
    var forgot = {
        init: function () {
            var old = false;//旧密码
            var newp = false;//新密码
            var ispwd = false;//两次输入
            this.bindEvents();
        },
        bindEvents: function () {
            var self = this;
            //旧密码验证start
            $("#old-pwd").blur(function () {
                self.isPwdNull();
            });
            //新密码验证start
            $("#new-pwd").blur(function () {
                self.isnewPwdNull();
            });
            //两次输入密码是否一致验证
            $("#repeat-new-pwd").blur(function () {
               self.isPwd();
            });
            //确定按钮
            $(".pwd-btn").click(function () {
                self.submitPwd();
            });
            //关闭错误提示按钮
            $(".colse-tip-btn").click(function () {
                $(".pwd-tip-box").hide();
                if (APP) {
                    YiYi_APP.reLogin();
                } else {
                    window.location.href = "/customer/index";
                };
            });
        },
        //旧密码验证
        isPwdNull: function () {
            if ($("#old-pwd").val() == "") {
                $(".old-error").html("<font color='red'>*</font>");
                old = false;
            }
            else {
                old = true;
                $(".old-error").html("<font color='red'></font>");
            }
        },
        //新密码验证
        isnewPwdNull: function () {
            if ($("#new-pwd").val() == "") {
                $(".new-error").html("<font color='red'>*</font>");
                newp = false;
            }
            else {
                newp = true;
                $(".new-error").html("<font color='red'></font>");
            }
        },
        //两次输入密码是否一致验证
        isPwd: function () {
            if ($("#repeat-new-pwd").val() == "") {
                $(".repeat-error").html("<font color='red'>*</font>");
                ispwd = false;
            } else if ($("#repeat-new-pwd").val() != $("#new-pwd").val()) {
                $(".repeat-error").html("<font color='red'>"+_("不一致")+"</font>");
                ispwd = false;
            }
            else {
                ispwd = true;
                $(".repeat-error").html("<font color='red'></font>");
            }
        },
        //确定
        submitPwd: function () {
            var self = this;
            var oldpwd = $("#old-pwd").val();
            var newpwd = $("#new-pwd").val();
            var repeatnewpwd = $("#repeat-new-pwd").val();
            if (ispwd && old && newp) {
                self.customerModifyPassword();
            }
            else {
                $(".pwd-tip-box").show();
                $(".pwd-msg").html(_("请正确填写资料"));;
            }
        },
        //修改密码
        customerModifyPassword:function(){
            var requestCustomer = {};
            requestCustomer.CustomerID = customerId;
            requestCustomer.OldUserPassword = $("#old-pwd").val();
            requestCustomer.NewUserPassword = $("#new-pwd").val();
            $.ajax({
                url: "/Customer/CustomerModifyPassword",//提交地址
                type: "POST",//提交方式
                data: JSON.stringify(requestCustomer),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    var _result = eval('(' + result + ')');//eval()解析单个json对象
                    if (_result["msgcode"] == true) {
                        $(".pwd-tip-box").show();
                        $(".pwd-msg").html(_("修改成功！"));
                    } else {
                        $(".pwd-tip-box").show();
                        $(".pwd-msg").html(_(_result["msginfo"]));
                    }
                }
            });
        },
    };
    forgot.init();
})();
